/*
 * Copyright (c) 2001, 2003 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation. Boris Pruessmann -
 * Added safeGetFileType().
 */
package net.sourceforge.perforce.core.internal.api;

import net.sourceforge.perforce.core.api.FileType;
import net.sourceforge.perforce.core.api.IFileType;
import net.sourceforge.perforce.core.api.NullFileType;
import net.sourceforge.perforce.core.resources.IChangelist;
import net.sourceforge.perforce.core.resources.LocalFile;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

import java.util.Date;
import java.util.Map;

/**
 * Internal helper class.
 * 
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
final class Helper {
  // ------------------------------------------------------------------------
  // Private Constructors

  /** Constructor for Helper. Utility classes won't be instantiated. */
  private Helper() {
  }

  // -----------------------------------------------------------------------
  // Public Static Methods

  /**
   * Helper method returning either the associated string value or an empty
   * string.
   * 
   * @param map the map.
   * @param key the key.
   * @return the value.
   */
  public static final String safeGetString(Map<String, ?> map, String key) {
    String value = (String) map.get(key);
    return (value != null) ? value : ""; //$NON-NLS-1$
  }

  /**
   * Helper method either returning the associated date value or the current
   * time.
   * 
   * @param map the map.
   * @param key the key.
   * @return the value.
   */
  public static final Date safeGetDate(Map<String, ?> map, String key) {
    String value = safeGetString(map, key);
    return !value.equals("") ? decodeTime(value) : new Date(); //$NON-NLS-1$
  }

  /**
   * Helper method either returning the associated integer or 0.
   * 
   * @param map the map.
   * @param key the key.
   * @return the value.
   */
  public static final Integer safeGetInteger(Map<String, ?> map, String key) {
    String value = safeGetString(map, key);
    return !value.equals("") ? Integer.valueOf(value) : new Integer(0); //$NON-NLS-1$
  }

  /**
   * Helper method either returning the associated changelist number of -1 for
   * default.
   * 
   * @param map the map.
   * @param key the key.
   * @return the value.
   */
  public static final Integer safeGetChangelist(Map<String, ?> map, String key) {
    String value = safeGetString(map, key);
    return !value.equals("default") ? //$NON-NLS-1$
        Integer.valueOf(value) : IChangelist.DEFAULT;
  }

  /**
   * Retries the filetype from the map.
   * 
   * @param map the map.
   * @param key the key.
   * @return the IFileType object.
   */
  public static final IFileType safeGetFileType(Map<String, ?> map, String key) {
    final String stringValue = safeGetString(map, key);
    if (stringValue.equals("")) //$NON-NLS-1$
    {
      return new NullFileType();
    } else {
      return new FileType(stringValue);
    }
  }

  /**
   * Helper method for decoding the Perforce time format to a java.util.Date
   * object.
   * 
   * @param timeString the perforce time string.
   * @return the decoded date.
   */
  public static final Date decodeTime(String timeString) {
    long timeInt = Integer.parseInt(timeString);
    return new Date(timeInt * 1000);
  }

  /**
   * Returns an IFile instance representative of the supplied LocalFile.
   */
  public static IFile getIFileForLocalFile(LocalFile source) {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IFile src = root.getFile(getIPathForLocalFile(source));
    return src;
  }

  /**
   * Returns an IPath instance representative of the supplied LocalFile.
   */
  public static IPath getIPathForLocalFile(LocalFile file) {
    return new Path(file.getAbsolutePath());
  }
}
